package com.example.recyclerview

import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

/**
 recyclerview优化
 */
class MainActivity : AppCompatActivity() {
    private lateinit var rv:RecyclerView
    private val adapter:PostAdapter by lazy{
        PostAdapter(
            imageClick = {
                Toast.makeText(this,"点击饿了图片",Toast.LENGTH_SHORT).show()
            },
            onLikeClick = { position->
                Toast.makeText(this, "点击了点赞$position",Toast.LENGTH_SHORT).show()
                //爱心改变
                adapter.updateData(position)


            }
        )
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_main)
        initView()
        initData()

//        rv.setRecycledViewPool() 调整recyclerPool
//        rv.setItemViewCacheSize() 调整mCacheViews
    }


    private fun initView() {
        rv  = findViewById(R.id.rv)
        rv.adapter = adapter
        rv.layoutManager = LinearLayoutManager(this)
    }

    private fun initData() {
        val data = mutableListOf<PostEntity>()
        for (i in 1..200){
            data.add(PostEntity(id = i , title = "标题$i", content = "内容", image = "https://img2.baidu.com/it/u=2814429148,2262424695&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422", video = ""))
        }
        adapter.setData(data)
    }

    fun addData(view: View) {
        adapter.addData(PostEntity(id = 1000 , title = "标题hahahhaah", content = "内容", image = "https://img2.baidu.com/it/u=2814429148,2262424695&fm=253&fmt=auto&app=138&f=JPEG?w=800&h=1422", video = ""))
    }

    fun removeData(view:View){
        adapter.removeData(2)
    }


}